package com.menhai.personmanagement.services;

import com.menhai.personmanagement.entity.Frontpage;
import com.menhai.personmanagement.entity.SysNav;
import com.menhai.personmanagement.entity.SysRole;
import com.menhai.personmanagement.mapper.FrontpageMapper;
import com.menhai.personmanagement.mapper.SysNavMapper;
import com.menhai.personmanagement.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 首页 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-12-01
 */
 @Service
public class IFrontpageService{

   @Autowired
   private FrontpageMapper frontpageMapper;

   @Autowired
   private SysRoleMapper sysRoleMapper;

   @Autowired
   private SysNavMapper sysNavMapper;


   /**
    * @describe 首页版1
    * @param
    * @Return java.util.List<com.menhai.personmanagement.entity.Frontpage>
    * @Date 2021/12/21 10:04
    * @author shikun
    */
    public List<Frontpage> selectListByOrder(){
        return frontpageMapper.selectListByOrder();
    }

   /**
    * @describe
    * @param roleId
    * @Return java.util.List<com.menhai.personmanagement.entity.Frontpage>
    * @Date 2021/12/21 10:04
    * @author shikun
    */
   public String selectBylevels(String roleId,String level){

    //获取power
    SysRole sysRole = sysRoleMapper.selectById(roleId);

    //拆分字符串
    List<String> powerlist = new ArrayList<>(Arrays.asList(sysRole.getPowers().split("\\|")));

    //查询SysNav
    List<SysNav> sysNavList = sysNavMapper.sysroleBypowerslist(level);

    // powerlist 和 sysNavList 求交集 根据 SysNav 得 navId
    List<SysNav> sysNavs = sysNavList.stream().filter(a -> powerlist.contains(String.valueOf(a.getNavId()))).collect(Collectors.toList());

    if (sysNavs.size()>0){
        return sysNavs.get(0).getUrl();
    }else {
        return "null";
    }
   }

   /**
    * @describe 首页版2
    * @param roleId
    * @Return java.util.List<com.menhai.personmanagement.entity.Frontpage>
    * @Date 2021/12/28 10:02
    * @author shikun
    */
   public List<Frontpage> selectfixedpermissions(String roleId){

       SysRole sysRole = sysRoleMapper.selectById(roleId);

       //拆分字符串
       String[] powers = sysRole.getPowers().split("\\|");;

       //查询SysNavlist
       List<SysNav> sysNavList = sysNavMapper.sysroleBypowerslistfixed(powers);

       //拿到level得list
       List<String> parentids = sysNavList.stream().map(u -> u.getLevel()).collect(Collectors.toList());

       return  frontpageMapper.selectBylevels(parentids);
   }
}
